package com.cennavi.bigdata.baotou.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cennavi.bigdata.baotou.dao.CrontabDao;
import com.cennavi.bigdata.baotou.dao.impl.CrontabDaoImpl;
import com.cennavi.bigdata.baotou.service.CrontabService;
import com.cennavi.bigdata.baotou.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by cennavi on 2018/4/11.
 */
@Service
public class CrontabServiceImpl implements CrontabService {

    private static final Logger LOGGER =  LoggerFactory.getLogger(CrontabDaoImpl.class);
    @Autowired
    private CrontabDao crontabDao;

    @Override
    public void insertRoadIndexTravelInfo() throws Exception{

        List<JSONObject> list = crontabDao.getRoadIdById();
        boolean flg = crontabDao.insertRoadIndexTravelInfo(list);
        if (flg){
            System.out.println("定时任务执行成功，数据保存成功");
        }else {
            System.out.println("定时任务执行失败，数据保存失败");
        }
    }

    @Override
    public void deleteHistoryInfo() {

        //获取三个月前的时间
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH,-3);
        String dateStr = DateUtils.DateFormatUnit.DATE.getDateStr(instance.getTime());
        //根据时间删除表中数据（只保留三个月）
        boolean flg = crontabDao.deleteHistoryInfo(dateStr);
        if (flg){
            LOGGER.info("数据删除成功");
        }else {
            LOGGER.info("数据删除失败");
        }
    }

    @Override
    public String inserPgFromOracle(String start, String end) {

        //从oracle获取数据
        List<JSONObject> list = crontabDao.getInfoFromOracle(start, end);
        //存入pg库中
        try {
            boolean flg = crontabDao.insertRoadIndexTravelInfo(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveDayIndexFromOracleToPg(JSONObject jsonObject) {
        String time = jsonObject.getString("time");
        crontabDao.deleteDayIndexByTime(time);
        JSONArray road = jsonObject.getJSONArray("road");
        crontabDao.saveIntoRoadDayIndex(road);
        JSONArray area = jsonObject.getJSONArray("area");
        crontabDao.saveIntoAreaDayIndex(area);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveHourIndexFromOracleToPg(JSONObject jsonObject) {
        String time = jsonObject.getString("time");
        crontabDao.deleteHourIndexByTime(time);
        JSONArray roadmap = jsonObject.getJSONArray("road");
        crontabDao.saveIntoRoadHourIndex(roadmap);
        JSONArray areamap = jsonObject.getJSONArray("area");
        crontabDao.saveIntoAreaHourIndex(areamap);
    }

    @Override
    public void saveQuarter() {
        String dateStr = DateUtils.DateFormatUnit.DATE_AND_MONTH.getDateStr(new Date());
        String month = dateStr.substring(4);
        String year = dateStr.substring(0,4);
        JSONObject jsonObject = new JSONObject();
        if (month.equals("01")||month.equals("02")||month.equals("03")){
            jsonObject.put("id",year+"01");
            jsonObject.put("name",year+"年 第一季度");
        }else if (month.equals("04")||month.equals("05")||month.equals("06")){
            jsonObject.put("id",year+"02");
            jsonObject.put("name",year+"年 第二季度");
        }else if (month.equals("07")||month.equals("08")||month.equals("09")){
            jsonObject.put("id",year+"03");
            jsonObject.put("name",year+"年 第三季度");
        }else {
            jsonObject.put("id",year+"04");
            jsonObject.put("name",year+"年 第四季度");
        }
        crontabDao.saveQuarter(jsonObject);
    }

    @Override
    public void deleteRoadPredictHis() {
        crontabDao.deleteRoadPredictHis();
    }

}
